<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir("../");
$path3 = getcwd();
chdir(dirname(__FILE__));
#debug($path3 , $path2);
include_once $path3."/lib/core.class.php";



class EditPassClass extends IPlugin 
{
    var $color;
    
    function EditPassClass($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
        $this->color = "darkgray";
    }
    
    function getPluginTag() { return "editpass"; }
    function getPluginName() { return "Admin password editor Plugin"; }
    function getPluginVersion() { return "1.0.0"; }
    
    function getProperty($p = null)
    {
        if(null == $p)
        {
            return array("MenuBlockTitle", "MenuTitle");
        }
        $result = "";
        switch ($p)
        {
            case "MenuBlockTitle": { return "Administrator account"; } break;
            case "MenuTitle" : { return "Edit Password"; } break;
        };
        return $result;
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------

    function doAction($params)
    {
        if(null != ($result = IPlugin::doAction($params)))
        {
            return $result;
        }

        $arg = getCoreInstance();
        $arg = $arg->getGET();
        
        if("echo" == $arg["action"])
        {
            echo "NewslineList: echo: ".$arg["string"];
        }

        if("changepass" == $_POST["action"])
        {
            $z = $_POST;
            unset($z["action"]);
            $core = getCoreInstance();
            $db = $core->getProperty("db");
            $newpass = $_POST['pasword'];
                        $this->setadminpasw($newpass);

        }

        return true;
    }


    // ----------------------------------------------------------------------------------
    // --- [ Render pages ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------

    function Render($params)
    {
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }
        
        $core = getCoreInstance();
        $render = $core->framework->smarty;
        $render->assign("baseurl", "?".$this->getURL()."&page=".$_G["page"]);

        $page = "";

        switch ($_G["page"]) {
/*
            case "edit":
                $page = $this->renderNewslineEdit($render);
                break;
*/
            default:
                $page = $this->renderNewslineList($render);
                break;
        }

        return $render->fetch("file:".$this->tplfolder."/".$page);
    }
    
    
    // ----------------------------------------------------------------------------------
    // --- [ Generate Customers List ] --------------------------------------------------
    // ----------------------------------------------------------------------------------
    
    function renderNewslineList(&$render)
    {
        $core = getCoreInstance();
        $db = $core->getProperty("db");

        $render->assign("adminpass", $this->getadminpasw());
        $render->assign("adminlogin", 'admin');
        $render->assign("url", "?".$this->getURL());

        return "adminpass.tpl.php";
    }
    
    function getadminpasw(){

        $core = getCoreInstance();
        $db = $core->getProperty("db");

        return (db2str($db->getone("select password from users where permission = 99 and username = 'admin'")));

    }

    function setadminpasw($p){

        $core = getCoreInstance();
        $db = $core->getProperty("db");

        if ($db->exec("update users set password = '".str2db($p)."' where permission = 99 and username = 'admin'")){
        return 1;
        }
        else{
        return 0;
        }
        

    }


};


?>